#include "stdafx.h"
#include "FrameRate.h"
#include <Windows.h>


long FrameRate::s_currentFrame = 0;

FrameRate::FrameRate(void)
:mLastFrameTime(0),mStartFrameTime(0),mEndFrameTime(0),mOutterDelta(0),mDelta(0)
{
	mSecondPerFrame = 1.0/FrameRate::FRAME_RATE;
}

FrameRate::~FrameRate(void)
{}

void FrameRate::startFrame()
{
	mStartFrameTime = GetTime();
	mOutterDelta = mStartFrameTime - mLastFrameTime;
	mLastFrameTime = mStartFrameTime;
	s_currentFrame++;
	if (s_currentFrame<0)
	{
		s_currentFrame = 0;
	}
}

void FrameRate::endFrame()
{
	mEndFrameTime = GetTime();
	mDelta = mEndFrameTime - mStartFrameTime;
}

double FrameRate::getDelta() const
{
	return mDelta;
}

int FrameRate::getInnerFPS() const
{
	if (mDelta>0)
	{
		return (int)(1/mDelta);
	}
	else
	{
		return 0;
	}
}

int FrameRate::getOutterFPS() const
{
	return (int)(1/mOutterDelta);
}

void FrameRate::lockFrame() const
{
	while (GetTime()-mStartFrameTime < mSecondPerFrame)
	{		
	}
}

double FrameRate::GetTime() const
{
	LARGE_INTEGER integer;
	LARGE_INTEGER frequency;
	QueryPerformanceFrequency(&frequency);
	QueryPerformanceCounter(&integer);
	return (double)integer.QuadPart/frequency.QuadPart;
}